// components/goods-detail-gb-all-members-item/goods-detail-gb-all-members-item.js 拼团更多人员
var common_js = require("../../utils/common.js");

Component({
  /**
   * 组件的属性列表
   */
  properties: {
    icon: {
      type: String,
      value: ''
    },
    nikeName: {
      type: String,
      value: ''
    },
    number: {
      type: Number,
      value: ''
    },
    time: {
      type: String,
      value: ''
    },
    expire: {
      type: Number,
      value: '',
      observer(newVal, oldVal, changedPath) {
        if (newVal) {
          //初始化倒计时
          this.initTimer(newVal);
        }
      }
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    timerObj: { //倒计时
      timer: null,
      isEnd: false,
      endTime: '',
      item: null
    },
  },

  /**
   * 组件的方法列表
   */
  methods: {
    onPd: function() {
      this.triggerEvent('pd', null, {});
    },
    //初始化计时器
    initTimer: function(expire) {
      if (expire == 0) {
        var tmpTimerObj = this.data.timerObj;
        tmpTimerObj.hou = "00";
        tmpTimerObj.min = "00";
        tmpTimerObj.sec = "00";
        this.setData({
          timerObj: tmpTimerObj,
        });
        return;
      }
      var tmpTimerObj = this.data.timerObj;
      if (tmpTimerObj.timer) {
        clearTimeout(tmpTimerObj.timer);
      }
      tmpTimerObj.endTime = new Date().getTime() + expire * 1000;
      this.setData({
        timerObj: tmpTimerObj,
      });
      //start
      this.countTimer();
    },

    // 计时器**********
    countTimer() { //倒计时函数
      // 获取当前时间，同时得到活动结束时间数组
      let newTime = new Date().getTime();
      var tmpTimerObj = this.data.timerObj;
      let endTime = tmpTimerObj.endTime;;

      // 对结束时间进行处理渲染到页面
      let obj = null;
      // 如果活动未结束，对时间进行处理
      if (endTime - newTime > 0) {
        let time = (endTime - newTime) / 1000;
        // 获取天、时、分、秒
        let day = parseInt(time / (60 * 60 * 24));
        let dayHour = (day > 0) ? day * 24 : 0;
        let hou = dayHour + parseInt(time % (60 * 60 * 24) / 3600);
        let min = parseInt(time % (60 * 60 * 24) % 3600 / 60);
        let sec = parseInt(time % (60 * 60 * 24) % 3600 % 60);
        obj = {
          day: common_js.timeFormat(day),
          hou: common_js.timeFormat(hou),
          min: common_js.timeFormat(min),
          sec: common_js.timeFormat(sec)
        }
      } else { //活动已结束，全部设置为'00'
        obj = {
          day: '00',
          hou: '00',
          min: '00',
          sec: '00'
        }
        tmpTimerObj.isEnd = true;
      }
      tmpTimerObj.item = obj;
      //1s调用一次
      var that = this;
      var timerItem = setTimeout(function() {
        that.countTimer()
      }, 1000);
      //定时清除
      clearTimeout(this.data.timerObj.timer);
      if (tmpTimerObj.isEnd) {
        clearTimeout(timerItem);
        return;
      } else {
        tmpTimerObj.timer = timerItem;
      }
      // 渲染，然后每隔一秒执行一次倒计时函数
      this.setData({
        timerObj: tmpTimerObj
      })
    }
    // 计时器**********
  },
  //组件生命周期函数-在组件实例被从页面节点树移除时执行
  detached: function() {
    if (this.data.timerObj.timer) {
      clearTimeout(this.data.timerObj.timer);
    }
  }
})